package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.bo.ZhcaChartAddBo;
import com.ruoyi.system.bo.ZhcaChartEditBo;
import com.ruoyi.system.bo.ZhcaChartQueryBo;
import com.ruoyi.system.domain.ZhcaChart;
import com.ruoyi.system.mapper.ZhcaChartMapper;
import com.ruoyi.system.service.IZhcaChartService;
import com.ruoyi.system.vo.ZhcaChartVo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 图表Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-31
 */
@Service
public class ZhcaChartServiceImpl extends ServicePlusImpl<ZhcaChartMapper, ZhcaChart> implements IZhcaChartService {

	@Override
	public ZhcaChartVo queryById(Long id) {
		return getVoById(id, ZhcaChartVo.class);
	}

	@Override
	public TableDataInfo<ZhcaChartVo> queryPageList(ZhcaChartQueryBo bo) {
		PagePlus<ZhcaChart, ZhcaChartVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZhcaChartVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<ZhcaChartVo> queryList(ZhcaChartQueryBo bo) {
		return listVo(buildQueryWrapper(bo), ZhcaChartVo.class);
	}

	private LambdaQueryWrapper<ZhcaChart> buildQueryWrapper(ZhcaChartQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ZhcaChart> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getType() != null, ZhcaChart::getType, bo.getType());
		lqw.eq(StrUtil.isNotBlank(bo.getSeries()), ZhcaChart::getSeries, bo.getSeries());
		lqw.eq(bo.getData() != null, ZhcaChart::getData, bo.getData());
		lqw.eq(bo.getOrder() != null, ZhcaChart::getOrder, bo.getOrder());
		lqw.eq(StrUtil.isNotBlank(bo.getGroup()), ZhcaChart::getGroup, bo.getGroup());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(ZhcaChartAddBo bo) {
		ZhcaChart add = BeanUtil.toBean(bo, ZhcaChart.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(ZhcaChartEditBo bo) {
		ZhcaChart update = BeanUtil.toBean(bo, ZhcaChart.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(ZhcaChart entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	@Override
	public String importCharts(List<ZhcaChartVo> charts) {
		if (CollUtil.isEmpty(charts)) {
			return "很抱歉，导入失败！读取不到数据。";
		}
		Map<Integer, List<String>> typeMap = charts.stream().collect(Collectors.groupingBy(ZhcaChartVo::getType, Collectors.mapping(ZhcaChartVo::getGroup, Collectors.toList())));
		typeMap.forEach((key, value) -> {
			Integer type = Optional.ofNullable(key).orElseThrow(() -> new CustomException("很抱歉，导入失败！无图标类型。"));
			Set<String> groups = value.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
			//删除旧数据
			this.remove(Wrappers.<ZhcaChart>lambdaQuery().eq(ZhcaChart::getType, type).in(ZhcaChart::getGroup, groups));
		});
		//导入
		this.saveAll(BeanUtil.copyToList(charts, ZhcaChart.class));
		return "恭喜您，数据已全部导入成功！共 " + charts.size() + " 条";
	}

	@Override
	public List<String> selectGroups(Integer type) {
		List<Object> objs = this.listObjs(
			Wrappers.<ZhcaChart>query()
				.select("distinct `group`")
				.lambda()
				.eq(ZhcaChart::getType, type)
		);
		return objs.stream().map(Object::toString).collect(Collectors.toList());
	}
}
